//
//  FileController.m
//  FileSystemDemo
//
//  Created by 小草毋正 on 2018/1/9.
//  Copyright © 2018年 C-LOVE. All rights reserved.


#import "FileController.h"
#import "FileDetailController.h"
#import "CLFileManage.h"
#import "TxtController.h"
#import "ImageController.h"

@interface FileController ()<UITableViewDelegate, UITableViewDataSource>


@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, strong) NSArray<CLFile *> *files;

@end



@implementation FileController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableFooterView  = [[UIView alloc] init];
    
}


- (void)setCurrentFilePath:(NSString *)currentFilePath {
    _currentFilePath = currentFilePath;
    self.files = [CLFileManage clfilesForPath:currentFilePath showHidden:NO];
}
    

- (void)setFiles:(NSArray<CLFile *> *)files {
    _files = files;
    [self.tableView reloadData];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.files.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *fileCellIdeifier = @"fileCellIdeifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:fileCellIdeifier];
    if(!cell){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:fileCellIdeifier];
    }
    CLFile *file = [self.files objectAtIndex:indexPath.row];
    cell.imageView.image = file.image;
    cell.textLabel.text  = file.name;
    
    if(file.fileType == CLFileTypeFolder){
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    else {
        cell.accessoryType = UITableViewCellAccessoryDetailButton;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    CLFile *file = [self.files objectAtIndex:indexPath.row];
    if(file.fileType == CLFileTypeFolder){
        FileController *fileController = [[FileController alloc] init];
        fileController.currentFilePath = file.filePath;
        fileController.title = [file.filePath lastPathComponent];;
        [self.navigationController pushViewController:fileController animated:YES];
    }
   else if(file.fileType == CLFileTypeLRC || file.fileType == CLFileTypeMD || file.fileType == CLFileTypeCode ){
        TxtController *controller = [[TxtController alloc] init];
        controller.file = file;
        [self.navigationController pushViewController:controller animated:YES];
    }
   else if(file.fileType == CLFileTypeImage){
       ImageController *controller = [[ImageController alloc] init];
       controller.file = file;
       [self.navigationController pushViewController:controller animated:YES];
       
   }
    
}


- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
    FileDetailController *controller = [[FileDetailController alloc] init];
    CLFile *file = [self.files objectAtIndex:indexPath.row];
    controller.file = file;
    [self.navigationController pushViewController:controller animated:YES];
    controller.oprationBlock = ^(OperationType type, NSString *filePath) {
        switch (type) {
            case OperationTypeDelete: {  // 删除操作
                if([CLFileManage deleteAtPath:filePath]){
                    self.files = [CLFileManage clfilesForPath:self.currentFilePath showHidden:NO];
                }
            }
                break;
                
            default:
                break;
        }
    };
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end











